﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace SmartHotel.MVC.MyFilter
{
    /// <summary>
    /// 授权过滤器
    /// </summary>
    public class _2207BFilter : Attribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            //throw new NotImplementedException();
            //通过判断cookies 是否有值来确定当前用户是否登录
            var cookies  = context.HttpContext.Request.Cookies;
            if (cookies.Any(x => x.Key == "username"))
            {
                //存在说明登录了,不做任何处理
            }
            else {
                //没有cookie ,返回到登录页面
                context.Result = new RedirectResult("/member/login");

            }
        }
    }
}
